<?php
class Control_ServerList_defaultSeverID {
 	function defaultSeverID() {
 		$s_id = MooForm::request('s_id');
 		$defaultSeverID = MooForm::request('defaultSeverID');
 		
 		if(empty($defaultSeverID)) {
 			$return['code'] = 2;
 			$return['msg']  = "defaultSeverID不存在";
 			$this->backMsg($return);
 		}

 		$ServerlistDao = MooDao::get('Serverlist');
 		$ServerlistObj = $ServerlistDao->load($s_id);
 		if(!$ServerlistObj) {
 			$return['code'] = 3;
 			$return['msg']  = "保存失败,未s_id不存在";
 			$this->backMsg($return);
 		}
 		
 		$serverlistCfg 	= $ServerlistObj->s_serverlist;
 		$fileName 		= $ServerlistObj->s_filename;
 		$game 			= $ServerlistObj->s_game;
 		$platform 		= $ServerlistObj->s_platform;
 		
 		// 解析$serverlistCfg
 		$serverlistArr = MooJson::decode($serverlistCfg);
 		// 更新defaultSeverID
 		$serverlistArr['defaultSeverID'] = $defaultSeverID;
 		$newServerlistInfo = MooJson::encode($serverlistArr);
 		
 		// 写入数据库与 本地文件即可
 		$ServerlistObj->set('s_serverlist', $newServerlistInfo);
 		
 		$formatPath = ROOT_PATH . "/servistLists";
 		$formatFile = ROOT_PATH . "/servistLists/{$game}_{$platform}_$fileName";
		if(!MooFile::isExists($formatPath)) {
			MooFile::mkDir($formatPath);
		}
		
		// 每次保存 覆盖重写该文件
		$rs = MooFile::write($formatFile, $newServerlistInfo);
 		if(!$rs) {
 			$return['code'] = 4;
 			$return['msg']  = "保存失败,写入文件失败";
 			$this->backMsg($return);
 		}
 		
 		$return['code'] = 1;
 		$return['msg']  = "保存成功";
 		$this->backMsg($return);
 		
	}
	
	private function backMsg($return) {
		exit(json_encode($return));
	}
}
